/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.HisDiagnosis;
import com.jims.master.modules.doctor.service.HisDiagnosisService;
import com.jims.master.modules.doctor.utils.DiagnosisTreeTable;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 诊断Controller
 * @author chenxy
 * @version 2017-03-06
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/hisDiagnosis")
public class HisDiagnosisController extends BaseController {

	@Autowired
	private HisDiagnosisService hisDiagnosisService;
	
	@ModelAttribute
	public HisDiagnosis get(@RequestParam(required=false) String id) {
		HisDiagnosis entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hisDiagnosisService.get(id);
		}
		if (entity == null){
			entity = new HisDiagnosis();
		}
		return entity;
	}

	/**
	 * 入院诊断页面
	 * @param hisDiagnosis
	 * @param model
	 * @author 陈涛
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(HisDiagnosis hisDiagnosis,String visitId,String patientId, Model model,String type) {
		/*hisDiagnosis.setInoroutFlag("1");
		hisDiagnosis.setOrgId(UserUtils.getOrgId());
		hisDiagnosis.setVisitId(visitId);
		Page<HisDiagnosis> page = hisDiagnosisService.findPage(new Page<HisDiagnosis>(request, response), hisDiagnosis);
		model.addAttribute("page", page);
		model.addAttribute("visitId", visitId);
		return "modules/doctor/hisDiagnosisIndex";*/
//		if(!"1".equals(type)){
			hisDiagnosis.setVisitId("");
//		}
		List<HisDiagnosis> list=hisDiagnosisService.findList(hisDiagnosis);
		List<HisDiagnosis> treeList=new DiagnosisTreeTable(list).buildTree();
		if(treeList.size()==0){
			treeList=list;
		}
		model.addAttribute("treeList", treeList);
		model.addAttribute("visitId", visitId);
		model.addAttribute("patientId", patientId);
		return "modules/doctor/hisDiagnosisIndex";
	}

	/**
	 * 入院诊断添加页面
	 * @param hisDiagnosis
	 * @param model
	 * @author 陈涛
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(HisDiagnosis hisDiagnosis,String visitId,String parentId, Model model) {
		String userName = UserUtils.getUser().getName();
		model.addAttribute("hisDiagnosis", hisDiagnosis);
		model.addAttribute("visitId", hisDiagnosis.getVisitId());
		model.addAttribute("clinicId", hisDiagnosis.getClinicId());
		model.addAttribute("patientId", hisDiagnosis.getPatientId());
		model.addAttribute("userName", userName);
		model.addAttribute("parentId", parentId);
		model.addAttribute("time", DateUtils.getDate());
		return "modules/doctor/hisDiagnosisForm";
	}

	/**
	 * 诊断保存
	 * @param hisDiagnosis
	 * @param model
	 * @author 陈涛
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(HisDiagnosis hisDiagnosis,String visitId, Model model) {
		String orgId = UserUtils.getOrgId();
		hisDiagnosis.setOrgId(orgId);
		hisDiagnosis.setVisitId(visitId);
	    StringData data=new StringData();
		int i=hisDiagnosisService.save(hisDiagnosis);
        if(i>0){
            data.setCode("success");
            data.setData("保存诊断成功");
        }else{
            data.setCode("error");
            data.setData("保存诊断失败");
        }
        return data;
	}

	/**
	 * 诊断删除
	 * @param hisDiagnosis
	 * @return
	 * @author 陈涛
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(HisDiagnosis hisDiagnosis) {
	    StringData data=new StringData();
		int i=hisDiagnosisService.delete(hisDiagnosis);
        if(i>0){
            data.setCode("success");
            data.setData("删除诊断成功");
        }else{
            data.setCode("error");
            data.setData("删除诊断失败");
        }
		return data;
	}

}